from ivic.core import Config, project_path
from ivic.core import Logging
from ivic.vswitch.Server import ServerClass

logger = Logging.get_logger('ivic.vswitch.Plugin')

__obj = None

def init(daemon_config):
    config = Config.load(project_path('/etc/ivic/vswitch.yaml'))
    # this should dispatch 'ivic.vswitch.*' to individual log file
    Logging.get_logger('ivic.vswitch', filename = config['log_file'])

    logger.debug('initializing %s' % __name__)
    global __obj
    if __obj:
        logger.error('reinitialized is not supported')
        return

    try:
        external_address = daemon_config['external_address']
    except Exception, e:
        logger.fatal('unable to get external_address from daemon: %s' % str(e))

    __obj = ServerClass(external_address, config)

def fini():
    global __obj 
    del __obj

def soap_mods():
    global __obj
    return {'vswitch': __obj}

def wsgi_mods():
    return {}
